<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Voice;
use App\Services\Qiniuyun;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Webpatser\Uuid\Uuid;

class VoiceController extends Controller
{
    public function create(Request $request)
    {
        $rule = [
            'uuid' => 'required|string',
            'name' => 'required',
            'voice' => 'required',
        ];
        $message = [
            'uuid.required' => 'uuid不能为空',
            'name.required' => 'name字段不能为空',
            'voice.required' => 'voice字段不能为空',
        ];

        $valid = Validator::make($request->all(), $rule, $message);
        if ($valid->fails()) {
            $this->code = 1;
            $this->msg = $valid->errors()->first();
            return $this->json();
        }

//        try {
            $save_data = $request->only(['name']);
            $save_data['name'] = $request->name;
            $save_data['voice_url'] = $request->voice;

            if (isset($request->voice_id) && !empty($request->voice_id)){
                $model = new Voice();
                $model->data['where']['voice_id'] = $request->voice_id;
                $save_data['updated_at'] = date('Y-m-d H:i:s');
                $model->editInfo($save_data);

                $id = $request->id;
            }else{
                $save_data['uuid'] = $request->uuid;
                $save_data['voice_id'] = UUID::generate()->uuid_ordered;
                $id = Voice::add($save_data);
            }

//        }catch (\Exception $e){
//            $this->msg  = is_numeric($e->getCode())? $e->getMessage(): "操作失败";
//            $this->code = 1;
//        }
        return $this->json();
    }

    public function getList(Request $request)
    {
        $rule = [
            'uuid' => 'required|string',
        ];
        $message = [
            'uuid.required' => 'uuid不能为空',
        ];

        $valid = Validator::make($request->all(), $rule, $message);
        if ($valid->fails()) {
            $this->code = 1;
            $this->msg = $valid->errors()->first();
            return $this->json();
        }
        $statusText = [
            '0' => '制作中',
            '1' => '制作完成',
            '2' => '制作失败'
        ];
        $model = new Voice();
        $where[] = ['status','<',99];
        if (isset($request->uuid) && $request->uuid != ''){
            $where[] = ['uuid' ,$request->uuid];
        }

        $model->data['num'] = $request->num;
        $model->data['page'] = $request->page;
        $model->data['where'] = $where;
        $model->data['select'] = 'id,name,voice_id,voice_url,uuid,cost,status,done_time';
        $this->data = $model->getList();

        if($this->data['data']){
            foreach ($this->data['data'] as $key=>$item){
                if($item['voice_url']){
                    $this->data['data'][$key]['voice_url'] = Qiniuyun::getFile($item['voice_url']);
                }
                $this->data['data'][$key]['status_text'] = $statusText[$item['status']];
            }
        }
        return $this->json();
    }

}
